package com.leetcode.algorithm.y22.m06;

/**
 * 121. 买卖股票的最佳时机 
 * 
 * https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
 * 
 * @author jie.deng
 *
 */
class Question0121Solution01 {

	public int maxProfit(int[] prices) {
		int len = prices.length;
		if (len == 0) {
			return 0;
		}
		int maxProfit = 0;
		int min = prices[0];
		for (int i = 1; i < len; i++) {
			if (prices[i] < min) {
				min = prices[i];
			} else if (prices[i] - min > maxProfit) {
				maxProfit = prices[i] - min;
			}
		}
		return maxProfit;
	}

}